package org.balthie.demo.jdk.thread.threadlocal;

public class InheritableThreadLocalDemo {
//	父线程的InheritableThreadLocal成员变量值 可以被子线程（在父线程new的线程）获取到
	private static InheritableThreadLocal<String> inheritThreadLocal = new InheritableThreadLocal<>();
	
//	父线程的普通ThreadLocal成员变量值， 子线程获取不到
	private static ThreadLocal<String> threadLocal = new ThreadLocal<>();

	public static void main(String[] args) {
//		InheritableThreadLocal.set 会设置 到 Thread.inheritedThreadLocals map中
//		new Thread的时候，Thread.init方法会 把父线程的inheritedThreadLocals 复制给子线程
		inheritThreadLocal.set("mainThread inherit TL value");
		threadLocal.set("mainThread normal TL value");
		System.out.println( Thread.currentThread() + " 主线程 inherit TL value:" + inheritThreadLocal.get());
		System.out.println( Thread.currentThread() + " 主线程 normal TL value:" + threadLocal.get());
		Thread thread = new Thread(new Runnable() {
			@Override
			public void run() {
				String value = inheritThreadLocal.get();
				System.out.println(Thread.currentThread() +  " 子线程 获取父线程 inherit TL value:" + value);
				System.out.println(Thread.currentThread() +  " 子线程 获取父线程 normal TL value:" + threadLocal.get());
				
			}
		});
		thread.start();
		
	}
}
